package cn.itcast.service.mobileImpl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.*;
import cn.itcast.entity.Result;
import cn.itcast.mobile.MobileOrderService;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * Demo class
 *
 * @author gyc
 * @date 2021/10/17
 */
@Service
public class MobileOrderServiceImpl implements MobileOrderService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AddressDao addressDao;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public Result submitOrder(Map map) {
        String name = (String) map.get("name");
        String phone = (String) map.get("phoneNumber");
        String idCard = (String) map.get("idCard");
        Integer setmealId =  Integer.valueOf(String.valueOf(map.get("setmealId")));
        String date = (String) map.get("orderDate");
        Date oDate = null;
        try {
            oDate = DateUtils.parseString2Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer addressId = Integer.valueOf(String.valueOf(map.get("addressId")));
        //根据手机号查询用户信息
        Member member = memberDao.findByPhone(phone);
        //根据提交信息收集用户信息 (登录只需要了手机号,收集不了客户信息)
        if (member == null ){
            member = new Member();
            member.setPhoneNumber(phone);
            member.setName(name);
            member.setIdCard(idCard);
            member.setRegTime(DateUtils.getToday());
            Integer memberId = memberDao.save(member);
        }
        //2. 如果选择的日期已经约满, 不允许预约
        OrderSetting orderSetting = orderSettingDao.findOrderByDate(oDate);
        //orderSetting 为空 则设置默认值 保存
        if (orderSetting == null) {
            orderSetting= new OrderSetting();
            orderSetting.setOrderDate(oDate);
            orderSetting.setNumber(100);
            orderSetting.setReservations(0);
            orderSettingDao.add(orderSetting);
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if ( number - reservations == 0){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //所选日期无可预约人数
        if (orderSetting.getNumber() <= 0) {
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //4. 如果当前用户已经预约过该体检套餐, 不允许预约  (身份证相同即为同一用户)
        //根据当前用户id 获取其订单记录

        Order order = orderDao.findByMemberId(idCard,oDate);
        Date orderDate;
        if (order == null){
            order = new Order();
            orderDate = oDate;
            order.setSetmealId(setmealId);
        }else{
            orderDate = order.getOrderDate();
        }
        Integer orderSetmealId = order.getSetmealId();
        //如果预约时间与订单记录中的时间相同
        if (orderDate == oDate && idCard.equals(order.getIdCard())) {
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        //5. 预约成功后, 扣减可预约人数 , 跳转到预约成功, 支付页面
        orderSetting.setNumber(orderSetting.getNumber()-1);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.update(orderSetting);
        //修改用户预约信息
        order.setId(UUID.randomUUID().toString());
        order.setOrderType(Order.ORDERTYPE_TELEPHONE);
        order.setOrderDate(oDate);
        order.setSetmealId(setmealId);
        order.setMemberId(member.getId());
        order.setPhoneNumber(phone);
        order.setName(name);
        order.setAddressId(addressId);
        order.setIdCard(idCard);
        order.setOrderStatus(0);
        order.setPayStatus(0);
        orderDao.mobilesave(order);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,order.getId());
    }

    @Override
    public Result findOrderById(String id) {
        Order order = orderDao.findOrderById(id);
        if (order == null){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
        order.setAddress(addressDao.findById(order.getAddressId()));
        order.setSetmeal(setmealDao.findById(order.getSetmealId()));
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,order);
    }
}
